﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-15 10:23:08
 * 功能：关卡记录界面
**************************************************************************************/

using Game.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Game.UI.HallPanel
{
    public class RecordView : MonoBehaviour
    {
        private HallPanelView m_RootView;
        private List<RecordContainer> m_Containers;

        public void Init(HallPanelView rootView)
        {
            m_RootView = rootView;
            m_Containers = new List<RecordContainer>();
            PoolManager.Instance.CreatePool(HallPanelNotifications.RECORD_ITEM_POOL, rootView.GetRect(HallPanelNotifications.RecordContainer_RECT).gameObject, 100);
        }

        private void OnEnable()
        {
            StartCoroutine(CreateRecords());
        }

        private IEnumerator CreateRecords()
        {
            var parent = m_RootView.GetRect(HallPanelNotifications.RecordContent_RECT);
            var continers = CalculateContainers(BaseDataMgr.Instance.Cfg.level.maxLv);
            for (int i = 0; i < continers.Count; i++)
            {
                var obj = PoolManager.Instance.Get(HallPanelNotifications.RECORD_ITEM_POOL);
                obj.transform.SetParent(parent);
                obj.transform.localScale = Vector3.one;
                (obj.transform as RectTransform).anchoredPosition3D = Vector3.zero;
                var container = obj.GetComponent<RecordContainer>();
                LogManager.Log($"i:{i} min:{continers[i].min} max:{continers[i].max}", "创建记录容器");
                container.Init(continers[i].min, continers[i].max, m_RootView);
                m_Containers.Add(container);
                yield return null;
            }
        }

        public List<(int min, int max)> CalculateContainers(int totalLevels)
        {
            List<(int, int)> result = new List<(int, int)>();
            int currentLevel = 1;
            int containerIndex = 0;

            while (currentLevel <= totalLevels)
            {
                int levelsInThisContainer = containerIndex % 2 == 0 ? 3 : 2;
                levelsInThisContainer = Math.Min(levelsInThisContainer, totalLevels - currentLevel + 1);

                int minLevel = currentLevel;
                int maxLevel = currentLevel + levelsInThisContainer - 1;

                result.Add((minLevel, maxLevel));

                currentLevel += levelsInThisContainer;
                containerIndex++;
            }

            return result;
        }

        private void OnDisable()
        {
            foreach (var item in m_Containers)
            {
                item.Clear();
                PoolManager.Instance.Delete(HallPanelNotifications.RECORD_ITEM_POOL, item.gameObject);
            }
            m_Containers.Clear();
        }

        private void OnDestroy()
        {
            PoolManager.Instance.ClearPool(HallPanelNotifications.RECORD_ITEM_POOL);
        }
    }
}